<template>
  <div>
    <el-row>
      <el-col>
        <el-form :inline="true" :model="resourceFilter" @keyup.enter.native="getDataList()" size="small">
          <el-form-item label="服务名称">
            <el-input v-model="resourceFilter.resourceName" style="width: 215px" placeholder="服务名称"></el-input>
          </el-form-item>
          <el-form-item label="创建时间">
            <el-date-picker
                v-model="resourceFilter.useTimeRange"
                type="daterange"
                range-separator="至"
                start-placeholder="开始时间"
                end-placeholder="结束时间"></el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="success" @click="getDataList()">查询</el-button>
            <el-button type="primary" @click="createService">创建服务</el-button>
          </el-form-item>
        </el-form>
        <el-table
            :data="dataList"
            border
            v-loading="dataListLoading"
            @selection-change="selectionChangeHandle"
            style="width: 100%;">
          <el-table-column
              type="selection"
              header-align="center"
              align="center"
              width="50">
          </el-table-column>
          <el-table-column
              prop="resourceName"
              header-align="center"
              align="center"
              label="服务名称">
          </el-table-column>
          <el-table-column
              prop="resourceDescribe"
              header-align="center"
              align="center"
              label="服务描述">
          </el-table-column>
          <!--        <el-table-column-->
          <!--            prop="servState"-->
          <!--            header-align="center"-->
          <!--            align="center"-->
          <!--            label="开启/关闭">-->
          <!--          <template #default="scope">-->
          <!--            <el-switch-->
          <!--                @change="updateState(scope.row)"-->
          <!--                v-model="scope.row.servState"-->
          <!--                :active-value="0"-->
          <!--                :inactive-value="1">-->
          <!--            </el-switch>-->
          <!--          </template>-->
          <!--        </el-table-column>-->
          <el-table-column
              prop="visitRestriction"
              header-align="center"
              align="center"
              label="开放限制"
              :formatter="showServLimit">
          </el-table-column>
          <el-table-column
              prop="servKeyword"
              header-align="center"
              align="center"
              label="关键词">
          </el-table-column>
          <el-table-column
              prop="createdTime"
              header-align="center"
              align="center"
              label="发布时间">
          </el-table-column>
          <el-table-column
              fixed="right"
              header-align="center"
              align="center"
              width="150"
              label="操作">
            <template #default="scope">
              <el-button type="primary" size="mini" @click="updateService(scope.row)">编辑</el-button>
              <el-button type="primary" size="mini" @click="deleteService(scope.row.resourceId)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
            @size-change="sizeChangeHandle"
            @current-change="currentChangeHandle"
            :current-page="page.currentPage"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="page.pageSize"
            :total="page.total"
            layout="total, sizes, prev, pager, next, jumper">
        </el-pagination>
      </el-col>
    </el-row>
    <!-- 弹窗, 新增 / 修改 -->
    <service-updated v-if="serviceUpdatedVisible" ref="serviceUpdated" @refreshDataList="getDataList"></service-updated>
  </div>
</template>

<script>
import pickerShortcut from "@/mixins/picker-shortcut";
import {queryServiceList, deleteDataFile, updateDataFileState, queryDataFileListByCondition} from "@/api/data_center/file-service";
import $axios from '@/network'
import ServiceUpdated from './ServiceUpdated'
import {queryResourceItemPage} from "@/api/resources-center/service";

export default {
  name: "FileManageList",
  mixins: [pickerShortcut],
  data() {
    return {
      resourceFilter: {
        resourceName: '',
        resourceType: '5',
        useTimeRange: []
      },
      dataList: [],
      page: {
        currentPage: 1,
        pageSize: 10,
        total: 0
      },
      dataListLoading: false,
      dataListSelections: [],
      serviceUpdatedVisible: false
    }
  },
  components: {
    ServiceUpdated
  },
  created () {
    this.getDataList()
  },
  methods: {
    /**
     * {获取资源列表}
     *
     * @author: 宋康
     * @date: 2023/2/3
     */
    getDataList() {
      this._debounce(() => queryResourceItemPage(this.page.currentPage, this.page.pageSize, this.resourceFilter).then(res => {
        if (res.status) {
          const data = res.data;
          this.dataList = data.records;
          this.page.total = data.total;
        }
      }), 500);
    },
    // 每页数
    sizeChangeHandle (val) {
      this.page.pageSize = val
      this.page.currentPage = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.page.currentPage = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle (val) {
      this.dataListSelections = val
    },
    createService() {
      this.$router.push({
        name: 'FileManageCreate',
      })
    },
    updateService(row) {
      this.serviceUpdatedVisible = true
      this.$nextTick(() => {
        this.$refs.serviceUpdated.init(row.resourceId)
      })
    },
    deleteService(servId) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteDataFile(servId).then(res=>{
          if(res.status){
            this.$message({
              message: '删除成功',
              type: 'success'
            });
            // 刷新页面
            this.$router.go(0)
          }else {
            this.$message.error('删除失败');
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    updateState(serv){
      updateDataFileState(serv.servId, serv.servState).then(res=>{
        if(res.status){
          this.$message({
            message: '操作成功',
            type: 'success'
          });
        }else {
          this.$message.error('操作失败');
        }
      })
    },
    showServLimit(row, column, cellValue, index){
      if(cellValue==1){
        return '可见'
      }
      if(cellValue==2){
        return '条件可见'
      }
      if(cellValue==3){
        return '线下'
      }
    }
  }
}
</script>

<style scoped>

</style>
